/* 
 * Android Operation Queue
 * http://code.google.com/p/operation-queue/
 * 
 * Copyright (C) 2012 Dong-u Seo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.minz.opqueue.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.minz.opqueue.Operation;
import com.minz.opqueue.OperationMessageHandler;
import com.minz.opqueue.OperationQueue;

public class OpQueueTestActivity extends Activity {

	long lastMillTime;
	TextView resultTextView;
	EditText ipEdit;
	OperationQueue opQueue;
	String list;
	OperationMessageHandler handler = new OperationMessageHandler() {

		@Override
		protected void onQueueFinished() {
			new Thread(){
				@Override
				public void run() {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
					}
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							try{
								Log.e("OpQueueTestActivity", "onQueueFinished()");
								StringBuilder sb = new StringBuilder();
								for(int i = 50; i < 100 ; i++){
									opQueue.add( new OperationtTest( i ) );
									sb.append( i ).append( "," );
								}
								list = sb.toString();
								resultTextView.setText( list );
							}catch (Exception e) {
							}
						}
					});
					super.run();
				}
			}.start();

			opQueue.interrupt();
		}

		@Override
		protected void onAOperationFinished(int rest, int init) {

		}

		@Override
		protected void onAOperationCanceled(int rest, int init) {

		}
	};


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		resultTextView = (TextView) findViewById( R.id.resultTextView );
		opQueue = new OperationQueue();
		StringBuilder sb = new StringBuilder();

		opQueue.setHandler(handler);
		opQueue.start();
		for(int i = 0; i < 50 ; i++){
			opQueue.add( new OperationtTest( i ) );
			sb.append( i ).append( "," );
		}
		list = sb.toString();
		resultTextView.setText( list );


	}

	class OperationtTest extends Operation< Void , Void , Void >{

		int num ;
		public OperationtTest(int num){
			this.num = num;
		}

		@Override
		protected Void doInBackground( Void... params ) {
			int time = (int) (1000 * Math.random());
			try {
				Thread.sleep( time );
			} catch ( InterruptedException e ) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute( Void result ) {
			super.onPostExecute( result );
			Log.i("", "onPostExecute");
			if(num == 0){
				list = list.substring( 1 );
			}else{
				list = list.replace( String.format( ",%d," , num ) , "," );
			}
			resultTextView.setText( list );
		}
	}

}